home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / CFM-68K SDK / Interfaces / MixedMode.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-17  |  14.6 KB  |  448 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        MixedMode.h
  3.  
  4.      Contains:    Mixed Mode Manager Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Package:    Universal Interfaces 2.1.5
  8.  
  9.      Copyright:    © 1984-1997 by Apple Computer, Inc.
  10.                  All rights reserved.
  11.  
  12.      Bugs?:        If you find a problem with this file, use the Apple Bug Reporter
  13.                  stack.  Include the file and version information (from above)
  14.                  in the problem description and send to:
  15.                      Internet:    apple.bugs@applelink.apple.com
  16.                      AppleLink:    APPLE.BUGS
  17.  
  18. */
  19.  
  20. #ifndef __MIXEDMODE__
  21. #define __MIXEDMODE__
  22.  
  23.  
  24. #ifndef __TYPES__
  25. #include <Types.h>
  26. #endif
  27. /*    #include <ConditionalMacros.h>                                */
  28.  
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32.  
  33. #if PRAGMA_ALIGN_SUPPORTED
  34. #pragma options align=mac68k
  35. #endif
  36.  
  37. #if PRAGMA_IMPORT_SUPPORTED
  38. #pragma import on
  39. #endif
  40.  
  41.  
  42. enum {
  43.     kRoutineDescriptorVersion    = 7
  44. };
  45.  
  46. /* MixedModeMagic Magic Cookie/Trap number */
  47. enum {
  48.     _MixedModeMagic                = 0xAAFE
  49. };
  50.  
  51. /* MixedModeState Version for CFM68K Mixed Mode */
  52.  
  53. enum {
  54.     kCurrentMixedModeStateRecord = 1
  55. };
  56.  
  57. /* Calling Conventions */
  58. typedef unsigned short CallingConventionType;
  59.  
  60.  
  61. enum {
  62.     kPascalStackBased            = (CallingConventionType)0,
  63.     kCStackBased                = (CallingConventionType)1,
  64.     kRegisterBased                = (CallingConventionType)2,
  65.     kD0DispatchedPascalStackBased = (CallingConventionType)8,
  66.     kD1DispatchedPascalStackBased = (CallingConventionType)12,
  67.     kD0DispatchedCStackBased    = (CallingConventionType)9,
  68.     kStackDispatchedPascalStackBased = (CallingConventionType)14,
  69.     kThinkCStackBased            = (CallingConventionType)5
  70. };
  71.  
  72. /* ISA Types */
  73. typedef SInt8 ISAType;
  74.  
  75.  
  76. enum {
  77.     kM68kISA                    = (ISAType)0,
  78.     kPowerPCISA                    = (ISAType)1
  79. };
  80.  
  81. /* RTA Types */
  82. typedef SInt8 RTAType;
  83.  
  84.  
  85. enum {
  86.     kOld68kRTA                    = (RTAType)(0 << 4),
  87.     kPowerPCRTA                    = (RTAType)(0 << 4),
  88.     kCFM68kRTA                    = (RTAType)(1 << 4)
  89. };
  90.  
  91. #if GENERATINGPOWERPC
  92. #define GetCurrentISA() ((ISAType) kPowerPCISA)
  93. #define GetCurrentRTA() ((RTAType) kPowerPCRTA)
  94. #else
  95. #define GetCurrentISA() ((ISAType) kM68kISA)
  96. #if GENERATINGCFM
  97. #define GetCurrentRTA() ((RTAType) kCFM68kRTA)
  98. #else
  99. #define GetCurrentRTA() ((RTAType) kOld68kRTA)
  100. #endif
  101. #endif
  102. #define GetCurrentArchitecture() (GetCurrentISA() | GetCurrentRTA())
  103. enum {
  104.     kRegisterD0                    = 0,
  105.     kRegisterD1                    = 1,
  106.     kRegisterD2                    = 2,
  107.     kRegisterD3                    = 3,
  108.     kRegisterD4                    = 8,
  109.     kRegisterD5                    = 9,
  110.     kRegisterD6                    = 10,
  111.     kRegisterD7                    = 11,
  112.     kRegisterA0                    = 4,
  113.     kRegisterA1                    = 5,
  114.     kRegisterA2                    = 6,
  115.     kRegisterA3                    = 7,
  116.     kRegisterA4                    = 12,
  117.     kRegisterA5                    = 13,
  118.     kRegisterA6                    = 14,
  119. /* A7 is the same as the PowerPC SP */
  120.     kCCRegisterCBit                = 16,
  121.     kCCRegisterVBit                = 17,
  122.     kCCRegisterZBit                = 18,
  123.     kCCRegisterNBit                = 19,
  124.     kCCRegisterXBit                = 20
  125. };
  126.  
  127. typedef unsigned short registerSelectorType;
  128.  
  129. /* SizeCodes we use everywhere */
  130.  
  131. enum {
  132.     kNoByteCode                    = 0,
  133.     kOneByteCode                = 1,
  134.     kTwoByteCode                = 2,
  135.     kFourByteCode                = 3
  136. };
  137.  
  138. /* Mixed Mode Routine Records */
  139. typedef unsigned long ProcInfoType;
  140.  
  141. /* Routine Flag Bits */
  142. typedef unsigned short RoutineFlagsType;
  143.  
  144.  
  145. enum {
  146.     kProcDescriptorIsAbsolute    = (RoutineFlagsType)0x00,
  147.     kProcDescriptorIsRelative    = (RoutineFlagsType)0x01
  148. };
  149.  
  150. enum {
  151.     kFragmentIsPrepared            = (RoutineFlagsType)0x00,
  152.     kFragmentNeedsPreparing        = (RoutineFlagsType)0x02
  153. };
  154.  
  155. enum {
  156.     kUseCurrentISA                = (RoutineFlagsType)0x00,
  157.     kUseNativeISA                = (RoutineFlagsType)0x04
  158. };
  159.  
  160. enum {
  161.     kPassSelector                = (RoutineFlagsType)0x0,
  162.     kDontPassSelector            = (RoutineFlagsType)0x08
  163. };
  164.  
  165. enum {
  166.     kRoutineIsNotDispatchedDefaultRoutine = (RoutineFlagsType)0x0,
  167.     kRoutineIsDispatchedDefaultRoutine = (RoutineFlagsType)0x10
  168. };
  169.  
  170. struct RoutineRecord {
  171.     ProcInfoType                    procInfo;                    /* calling conventions */
  172.     UInt8                            reserved1;                    /* Must be 0 */
  173.     ISAType                            ISA;                        /* Instruction Set Architecture */
  174.     RoutineFlagsType                routineFlags;                /* Flags for each routine */
  175.     ProcPtr                            procDescriptor;                /* Where is the thing we’re calling? */
  176.     UInt32                            reserved2;                    /* Must be 0 */
  177.     UInt32                            selector;                    /* For dispatched routines, the selector */
  178. };
  179. typedef struct RoutineRecord RoutineRecord;
  180.  
  181. typedef RoutineRecord *RoutineRecordPtr, **RoutineRecordHandle;
  182.  
  183. /* Mixed Mode Routine Descriptors */
  184. /* Definitions of the Routine Descriptor Flag Bits */
  185. typedef UInt8 RDFlagsType;
  186.  
  187.  
  188. enum {
  189.     kSelectorsAreNotIndexable    = (RDFlagsType)0x00,
  190.     kSelectorsAreIndexable        = (RDFlagsType)0x01
  191. };
  192.  
  193. /* Routine Descriptor Structure */
  194. struct RoutineDescriptor {
  195.     UInt16                            goMixedModeTrap;            /* Our A-Trap */
  196.     SInt8                            version;                    /* Current Routine Descriptor version */
  197.     RDFlagsType                        routineDescriptorFlags;        /* Routine Descriptor Flags */
  198.     UInt32                            reserved1;                    /* Unused, must be zero */
  199.     UInt8                            reserved2;                    /* Unused, must be zero */
  200.     UInt8                            selectorInfo;                /* If a dispatched routine, calling convention, else 0 */
  201.     UInt16                            routineCount;                /* Number of routines in this RD */
  202.     RoutineRecord                    routineRecords[1];            /* The individual routines */
  203. };
  204. typedef struct RoutineDescriptor RoutineDescriptor;
  205.  
  206. typedef RoutineDescriptor *RoutineDescriptorPtr, **RoutineDescriptorHandle;
  207. /* 68K MixedModeStateRecord */
  208. struct MixedModeStateRecord {
  209.     UInt32                             state1;
  210.     UInt32                             state2;
  211.     UInt32                             state3;
  212.     UInt32                             state4;
  213. };
  214. typedef struct MixedModeStateRecord MixedModeStateRecord;
  215.  
  216. #define BUILD_ROUTINE_DESCRIPTOR(procInfo, procedure)  \
  217.     {                                \
  218.     _MixedModeMagic,                \
  219.     kRoutineDescriptorVersion,        \
  220.     kSelectorsAreNotIndexable,        \
  221.     0,                                \
  222.     0,                                \
  223.     0,                                \
  224.     0,                                \
  225.     {                                \
  226.     {                                \
  227.     (procInfo),                    \
  228.     0,                                \
  229.     GetCurrentArchitecture(),     \
  230.     kProcDescriptorIsAbsolute |     \
  231.     kFragmentIsPrepared |            \
  232.     kUseNativeISA,                    \
  233.     (ProcPtr)(procedure),            \
  234.     0,                                \
  235.     0                                \
  236.     }                                \
  237.     }                                \
  238.     }
  239. #define BUILD_FAT_ROUTINE_DESCRIPTOR(m68kProcInfo, m68kProcPtr, powerPCProcInfo, powerPCProcPtr)  \
  240.     {                                \
  241.     _MixedModeMagic,                \
  242.     kRoutineDescriptorVersion,        \
  243.     kSelectorsAreNotIndexable,        \
  244.     0,                                \
  245.     0,                                \
  246.     0,                                \
  247.     1,                                \
  248.     {                                \
  249.     {                                \
  250.     (m68kProcInfo),                \
  251.     0,                                \
  252.     kM68kISA |                        \
  253.     kOld68kRTA,                    \
  254.     kProcDescriptorIsAbsolute |     \
  255.     kUseCurrentISA,                \
  256.     (ProcPtr)(m68kProcPtr),        \
  257.     0,                                \
  258.     0,                                \
  259.     },                                \
  260.     {                                \
  261.     (powerPCProcInfo),                \
  262.     0,                                \
  263.     GetCurrentArchitecture(),     \
  264.     kProcDescriptorIsAbsolute |     \
  265.     kFragmentIsPrepared |            \
  266.     kUseCurrentISA,                \
  267.     (ProcPtr)(powerPCProcPtr),        \
  268.     0,                                \
  269.     0                                \
  270.     }                                \
  271.     }                                \
  272.     }
  273.  
  274. enum {
  275. /* Calling Convention Offsets */
  276.     kCallingConventionWidth        = 4,
  277.     kCallingConventionPhase        = 0,
  278.     kCallingConventionMask        = 0xF,
  279. /* Result Offsets */
  280.     kResultSizeWidth            = 2,
  281.     kResultSizePhase            = kCallingConventionWidth,
  282.     kResultSizeMask                = 0x30,
  283. /* Parameter offsets & widths */
  284.     kStackParameterWidth        = 2,
  285.     kStackParameterPhase        = (kCallingConventionWidth + kResultSizeWidth),
  286.     kStackParameterMask            = 0xFFFFFFC0,
  287. /* Register Result Location offsets & widths */
  288.     kRegisterResultLocationWidth = 5,
  289.     kRegisterResultLocationPhase = (kCallingConventionWidth + kResultSizeWidth),
  290. /* Register Parameter offsets & widths */
  291.     kRegisterParameterWidth        = 5,
  292.     kRegisterParameterPhase        = (kCallingConventionWidth + kResultSizeWidth + kRegisterResultLocationWidth),
  293.     kRegisterParameterMask        = 0x7FFFF800,
  294.     kRegisterParameterSizePhase    = 0,
  295.     kRegisterParameterSizeWidth    = 2,
  296.     kRegisterParameterWhichPhase = kRegisterParameterSizeWidth,
  297.     kRegisterParameterWhichWidth = 3,
  298. /* Dispatched Stack Routine Selector offsets & widths */
  299.     kDispatchedSelectorSizeWidth = 2,
  300.     kDispatchedSelectorSizePhase = (kCallingConventionWidth + kResultSizeWidth),
  301. /* Dispatched Stack Routine Parameter offsets */
  302.     kDispatchedParameterPhase    = (kCallingConventionWidth + kResultSizeWidth + kDispatchedSelectorSizeWidth),
  303. /* Special Case offsets & widths */
  304.     kSpecialCaseSelectorWidth    = 6,
  305.     kSpecialCaseSelectorPhase    = kCallingConventionWidth,
  306.     kSpecialCaseSelectorMask    = 0x3F0,
  307. /* Component Manager Special Case offsets & widths */
  308.     kComponentMgrResultSizeWidth = 2,
  309.     kComponentMgrResultSizePhase = kCallingConventionWidth + kSpecialCaseSelectorWidth, /* 4 + 6 = 10 */
  310.     kComponentMgrParameterWidth    = 2,
  311.     kComponentMgrParameterPhase    = kComponentMgrResultSizePhase + kComponentMgrResultSizeWidth /* 10 + 2 = 12 */
  312. };
  313.  
  314. #define SIZE_CODE(size)         \
  315.     (((size) == 4) ? kFourByteCode : (((size) == 2) ? kTwoByteCode : (((size) == 1) ? kOneByteCode : 0)))
  316. #define RESULT_SIZE(sizeCode)     \
  317.     ((ProcInfoType)(sizeCode) << kResultSizePhase)
  318. #define STACK_ROUTINE_PARAMETER(whichParam, sizeCode)  \
  319.     ((ProcInfoType)(sizeCode) << (kStackParameterPhase + (((whichParam) - 1) * kStackParameterWidth)))
  320. #define DISPATCHED_STACK_ROUTINE_PARAMETER(whichParam, sizeCode)  \
  321.     ((ProcInfoType)(sizeCode) << (kDispatchedParameterPhase + (((whichParam) - 1) * kStackParameterWidth)))
  322. #define DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(sizeCode)  \
  323.     ((ProcInfoType)(sizeCode) << kDispatchedSelectorSizePhase)
  324. #define REGISTER_RESULT_LOCATION(whichReg)  \
  325.     ((ProcInfoType)(whichReg) << kRegisterResultLocationPhase)
  326. #define REGISTER_ROUTINE_PARAMETER(whichParam, whichReg, sizeCode)  \
  327.     ((((ProcInfoType)(sizeCode) << kRegisterParameterSizePhase) | ((ProcInfoType)(whichReg) << kRegisterParameterWhichPhase)) <<  \
  328.     (kRegisterParameterPhase + (((whichParam) - 1) * kRegisterParameterWidth)))
  329. #define COMPONENT_MGR_RESULT_SIZE(sizeCode)  \
  330.     ((ProcInfoType)(sizeCode) << kComponentMgrResultSizePhase)
  331. #define COMPONENT_MGR_PARAMETER(whichParam, sizeCode)  \
  332.     ((ProcInfoType)(sizeCode) << (kComponentMgrParameterPhase + (((whichParam) - 1) * kComponentMgrParameterWidth)))
  333. #define SPECIAL_CASE_PROCINFO(specialCaseCode)  \
  334.     (kSpecialCase | ((ProcInfoType)(specialCaseCode) << 4))
  335. enum {
  336.     kSpecialCase                = (CallingConventionType)0x0000000F
  337. };
  338.  
  339. enum {
  340. /* all of the special cases enumerated.  The selector field is 6 bits wide */
  341.     kSpecialCaseHighHook        = 0,
  342.     kSpecialCaseCaretHook        = 0,                            /* same as kSpecialCaseHighHook */
  343.     kSpecialCaseEOLHook            = 1,
  344.     kSpecialCaseWidthHook        = 2,
  345.     kSpecialCaseTextWidthHook    = 2,                            /* same as kSpecialCaseWidthHook */
  346.     kSpecialCaseNWidthHook        = 3,
  347.     kSpecialCaseDrawHook        = 4,
  348.     kSpecialCaseHitTestHook        = 5,
  349.     kSpecialCaseTEFindWord        = 6,
  350.     kSpecialCaseProtocolHandler    = 7,
  351.     kSpecialCaseSocketListener    = 8,
  352.     kSpecialCaseTERecalc        = 9,
  353.     kSpecialCaseTEDoText        = 10,
  354.     kSpecialCaseGNEFilterProc    = 11,
  355.     kSpecialCaseMBarHook        = 12,
  356.     kSpecialCaseComponentMgr    = 13
  357. };
  358.  
  359. /*
  360.     NOTES ON USING New[Fat]RoutineDescriptor[Trap] 
  361.     
  362.     When calling these routine from classic 68k code there are two possible intentions.
  363.  
  364.     The first is source compatibility with code ported to CFM (either PowerPC or 68k CFM). When
  365.     the code is compiled for CFM the functions create routine descriptors that can be used by
  366.     the mixed mode manager operating on that machine. When the code is compiled for classic 68k
  367.     these functions do nothing so that the code will run on Macintoshes that do not have a
  368.     mixed mode manager. The dual nature of these functions is achieved by turning the CFM calls
  369.     into "no-op" macros for classic 68k: You can put "NewRoutineDescriptor" in your source,
  370.     compile it for any runtime or instruction set architecture, and it will run correctly on the
  371.     intended runtime/instruction platform. All without source changes and/or conditional source.
  372.     
  373.     The other intention is for code that "knows" that it is executing as classic 68k runtime
  374.     and is specifically trying to call code of another architecture using mixed mode. Since the
  375.     routines were designed with classic <-> CFM source compatibility in mind this second case
  376.     is treated special. For classic 68k code to create routines descriptors for use by mixed mode
  377.     it must call the "Trap" versions of the routines (NewRoutineDescriptorTrap). These versions
  378.     are only available to classic 68k callers: rigging the interfaces to allow calling them
  379.     from CFM code will result in runtime failure because no shared library implements or exports
  380.     the functions.
  381.     
  382.  
  383.     This almost appears seamless until you consider "fat" routine descriptors and the advent of
  384.     CFM-68K. What does "fat" mean? CFM-68K is not emulated on PowerPC and PowerPC is not emulated
  385.     on CFM-68K. It makes no sense to create a routine descriptor having both a CFM-68K routine
  386.     and a PowerPC native routine pointer. Therefore "fat" is defined to be a mix of classic and
  387.     CFM for the hardware's native instruction set: on PowerPC fat is classic and PowerPC native,
  388.     on a 68k machine with CFM-68K installed fat is classic and CFM-68K.
  389.     
  390.     By definition fat routine descriptors are only constructed by code that is aware of the 
  391.     architecture it is executing as and that another architecture exists. Source compatibility
  392.     between code intented as pure classic and pure CFM is not an issue and so NewFatRoutineDescriptor
  393.     is not available when building pure classic code.
  394.     
  395.     NewFatRoutineDescriptorTrap is available to classic code on both PowerPC and CFM-68K. The
  396.     classic code can use the code fragment manager routine "FindSymbol" to obtain the address of 
  397.     a routine in a shared library and then construct a routine descriptor with both the CFM routine 
  398.     and classic    routine.
  399. */    
  400.  
  401. #if GENERATINGCFM
  402. extern pascal UniversalProcPtr NewRoutineDescriptor(ProcPtr theProc, ProcInfoType theProcInfo, ISAType theISA);
  403. extern pascal void DisposeRoutineDescriptor(UniversalProcPtr theProcPtr);
  404. extern pascal UniversalProcPtr NewFatRoutineDescriptor(ProcPtr theM68kProc, ProcPtr thePowerPCProc, ProcInfoType theProcInfo);
  405. #else
  406. #define DisposeRoutineDescriptor(theProcPtr)
  407. #define NewRoutineDescriptor(theProc, theProcInfo, theISA) ((UniversalProcPtr)theProc)
  408. /* Note that the call to NewFatRoutineDescriptor is undefined when GENERATINGCFM is false. */
  409.  
  410. extern pascal UniversalProcPtr NewRoutineDescriptorTrap(ProcPtr theProc, ProcInfoType theProcInfo, ISAType theISA)
  411.  TWOWORDINLINE(0x7000, 0xAA59);
  412. extern pascal void DisposeRoutineDescriptorTrap(UniversalProcPtr theProcPtr)
  413.  TWOWORDINLINE(0x7001, 0xAA59);
  414. extern pascal UniversalProcPtr NewFatRoutineDescriptorTrap(ProcPtr theM68kProc, ProcPtr thePowerPCProc, ProcInfoType theProcInfo)
  415.  TWOWORDINLINE(0x7002, 0xAA59);
  416. #endif
  417.  
  418. #if GENERATINGCFM
  419. /*
  420.  CallUniversalProc is only implemented in shared libraries on 68k and PowerPC, it is now
  421.  conditionalize with GENERATINGCFM.  This will catch accidental calls from classic 68K code
  422.  that previously only showed up as linker errors.
  423. */
  424. extern long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
  425. extern long CallOSTrapUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
  426. #endif
  427. #if GENERATING68K
  428. extern pascal OSErr SaveMixedModeState(MixedModeStateRecord *stateStorage, UInt32 stateVersion)
  429.  TWOWORDINLINE(0x7003, 0xAA59);
  430. extern pascal OSErr RestoreMixedModeState(MixedModeStateRecord *stateStorage, UInt32 stateVersion)
  431.  TWOWORDINLINE(0x7004, 0xAA59);
  432. #endif
  433.  
  434.  
  435. #if PRAGMA_IMPORT_SUPPORTED
  436. #pragma import off
  437. #endif
  438.  
  439. #if PRAGMA_ALIGN_SUPPORTED
  440. #pragma options align=reset
  441. #endif
  442.  
  443. #ifdef __cplusplus
  444. }
  445. #endif
  446.  
  447. #endif /* __MIXEDMODE__ */
  448.